package com.atguigu.controller;

import com.atguigu.utils.UUIDUtils;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("server")
/**
 * 跨越服务器的文件上传
 */
public class ServerController {
    @RequestMapping("uploadServer")
    public String uploadServer(MultipartFile multipartFile) throws IOException {
        String originalFilename = multipartFile.getOriginalFilename();
        //获取上传文件的后缀名
        String extension = FilenameUtils.getExtension(originalFilename);
        //定义新的文件名
        String fileName = "atguigu"+ System.currentTimeMillis()+ UUIDUtils.getUUID() +"."+extension;
        //创建客户端对象
        Client client = Client.create();
        //上传服务器的URL地址
        String url = "http://localhost:8081/xxx/upload/";
        //创建Web资源对象，传递上传的服务器地址
        WebResource webResource = client.resource(url+fileName);
        //通过资源对象上传，调用方法put
        //传递要上传文件的字节数组,传递字符串class对象
        //put方法的返回值，就是字符串类型
        String str = webResource.put(String.class,multipartFile.getBytes());
        System.out.println("str = " + str);
        return "forward:/success.jsp";
    }
}
